//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls {

	/// <summary>
	/// Summary description for PostModerationHistory.
	/// </summary>
	public class PostModerationHistory: TemplatedWebControl {

        #region Child Controls
        protected Repeater auditHistoryRepeater;
		protected ForumPager pager;
		protected CurrentPage currentPage;
        #endregion

		#region Skin
		protected override String ExternalSkinFileName {
			get {
				return "Moderation/View-PostModerationHistory.ascx";
			}
		}

		protected override void AttachChildControls () {
			auditHistoryRepeater = (Repeater) FindControl( "AuditHistoryRepeater") ;
			currentPage = (CurrentPage) FindControl( "CurrentPage" );
			pager = (ForumPager) FindControl( "Pager" );

			InitializeChildControls();
		}

		private void InitializeChildControls () {
			auditHistoryRepeater.ItemDataBound += new RepeaterItemEventHandler( AuditHistoryRepeater_ItemDataBound );

			pager.PageSize = CSContext.Current.SiteSettings.MembersPerPage;
		}
		#endregion
        
        protected override void OnLoad (EventArgs e) {
            if (!Page.IsPostBack) {
                this.DataBind();
            }

            base.OnLoad( e );
        }

        public override void DataBind () {
            base.DataBind();

            AuditSet auditSet = Audit.GetAuditHistoryForPost( CSContext.Current.PostID, pager.PageIndex, pager.PageSize );

            auditHistoryRepeater.DataSource = auditSet.Records;
            auditHistoryRepeater.DataBind();

            pager.TotalRecords = currentPage.TotalRecords = auditSet.TotalRecords;
            currentPage.TotalPages = pager.CalculateTotalPages();
            currentPage.PageIndex = pager.PageIndex;
        }

		private void AuditHistoryRepeater_ItemDataBound (Object sender, RepeaterItemEventArgs e) {
			AuditItem dataItem = (AuditItem) e.Item.DataItem;

			switch (e.Item.ItemType) {
				case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    HyperLink ItemUsernameLink = (HyperLink)e.Item.FindControl( "ItemUsernameLink" );
                    if (ItemUsernameLink != null) {
                        if (dataItem.ModeratorID > 0 && dataItem.ModeratorName.Length > 0) {
                            ItemUsernameLink.NavigateUrl = Globals.GetSiteUrls().ControlPanelUserEdit( dataItem.ModeratorID );
                            ItemUsernameLink.Text = dataItem.ModeratorName;
                        } else { 
                            ItemUsernameLink.NavigateUrl = null;
                            ItemUsernameLink.Text = ResourceManager.GetString( "NotAvailable" );
                        }
                    }

                    Label ItemAction = (Label)e.Item.FindControl( "ItemAction" );
                    if (ItemAction != null)
                        ItemAction.Text = Formatter.FormatModeratorAction( dataItem.Action );

                    Label ItemNotes = (Label)e.Item.FindControl( "ItemNotes" );
                    if (ItemNotes != null) {
                        if (Globals.IsNullorEmpty( dataItem.Notes ))
                            ItemNotes.Text = ResourceManager.GetString( "NotAvailable" );
                        else
                            ItemNotes.Text = Globals.HtmlDecode( dataItem.Notes );
                    }

                    Label ItemDate = (Label)e.Item.FindControl( "ItemDate" );
                    if (ItemDate != null)
                        ItemDate.Text = dataItem.DateModerated.ToString( CSContext.Current.User.Profile.DateFormat + "<br />hh:mm:ss tt");
					break;
			}
		}

	}
}
